# coding:utf-8
"""
description:Mode页面
"""
import os
import random
import time
from datetime import datetime

from appium.webdriver.common import mobileby

from common.Get_Yaml import get_yaml
from common.Log import LOG
from config.GlobalParameter import TEST_Element_YAML
from pages.Devices.ModeSchedule_page import ModeSchedulePublic_page

Data = get_yaml(os.path.join(TEST_Element_YAML, 'Devices', 'Mode.yaml'))


class Mode_page(ModeSchedulePublic_page):
    by = mobileby.MobileBy()

    """添加Device mode用。点击Sunday, Monday, Tuesday, Wednesday, Thursday, 
    Friday, Saturday后，分别显示once、weekends、 weekdays、everyday。"""
    AddModeRepeat_loc = (by.ID, Data.get_elementInfo(0))
    """Sunday"""
    AddRepeat1_loc = (by.ID, Data.get_elementInfo(1))
    """Monday"""
    AddRepeat2_loc = (by.ID, Data.get_elementInfo(2))
    """Tuesday"""
    AddRepeat3_loc = (by.ID, Data.get_elementInfo(3))
    """Wednesday"""
    AddRepeat4_loc = (by.ID, Data.get_elementInfo(4))
    """Thurday"""
    AddRepeat5_loc = (by.ID, Data.get_elementInfo(5))
    """Friday"""
    AddRepeat6_loc = (by.ID, Data.get_elementInfo(6))
    """Saturday"""
    AddRepeat7_loc = (by.ID, Data.get_elementInfo(7))

    def get_AddModeRepeat(self):
        """获取repeat选中的后文字显示"""
        return self.find_element(*self.AddModeRepeat_loc).text

    def click_AddModRepeat(self, times):
        """
        根据点击几次，来随机选择日期
        :param times:点击次数
        :return:
        """
        for i in range(times):
            nums = random.randint(1, 7)
            if nums == 1:
                self.find_element(*self.AddRepeat1_loc).click()
            elif nums == 2:
                self.find_element(*self.AddRepeat2_loc).click()
            elif nums == 3:
                self.find_element(*self.AddRepeat3_loc).click()
            elif nums == 4:
                self.find_element(*self.AddRepeat4_loc).click()
            elif nums == 5:
                self.find_element(*self.AddRepeat5_loc).click()
            elif nums == 6:
                self.find_element(*self.AddRepeat6_loc).click()
            else:
                self.find_element(*self.AddRepeat7_loc).click()

    """添加mode的save按钮"""
    AddModeSave_loc = (by.ID, Data.get_elementInfo(8))

    def click_AddModeSave(self):
        """添加mode的save按钮"""
        self.find_element(*self.AddModeSave_loc).click()

    """添加mode的Mode name"""
    AddModeName_loc = (by.ID, Data.get_elementInfo(9))

    def click_AddModeName(self):
        """添加mode的Mode name"""
        self.find_element(*self.AddModeName_loc).click()

    def get_AddModeName(self):
        """获取已选择mode的Mode name"""
        return self.find_element(*self.AddModeName_loc).text

    """添加mode的Start Time (Open)，有morning-6：00，evening-17：00，sleep-22：00"""
    AddModeStartTime_loc = (by.ID, Data.get_elementInfo(10))

    def get_AddModeStartTime(self):
        """添加mode的Start Time (Open)，有morning-6：00，evening-17：00，sleep-22：00"""
        return self.find_element(*self.AddModeStartTime_loc)

    """添加mode的End Time (Close)，有morning-9：00，evening-22：00，sleep-22：30"""
    AddModeEndTime_loc = (by.ID, Data.get_elementInfo(11))

    def get_AddModeEndTime(self):
        """添加mode的End Time (Close)，有morning-9：00，evening-22：00，sleep-22：30"""
        return self.find_element(*self.AddModeEndTime_loc)

    """编辑Device mode的元素。点击Sunday, Monday, Tuesday, Wednesday, Thursday, 
    Friday, Saturday后，分别显示once、weekends、 weekdays、everyday。"""
    EditModeRepeat_loc = (by.ID, Data.get_elementInfo(12))
    """Sunday"""
    EditRepeat1_loc = (by.ID, Data.get_elementInfo(13))
    """Monday"""
    EditRepeat2_loc = (by.ID, Data.get_elementInfo(14))
    """Tuesday"""
    EditRepeat3_loc = (by.ID, Data.get_elementInfo(15))
    """Wednesday"""
    EditRepeat4_loc = (by.ID, Data.get_elementInfo(16))
    """Thurday"""
    EditRepeat5_loc = (by.ID, Data.get_elementInfo(17))
    """Friday"""
    EditRepeat6_loc = (by.ID, Data.get_elementInfo(18))
    """Saturday"""
    EditRepeat7_loc = (by.ID, Data.get_elementInfo(19))

    def get_EditModeRepeat(self):
        """获取repeat选中的后文字显示"""
        return self.find_element(*self.EditModeRepeat_loc).text

    def click_EditModRepeat(self, times):
        """
        根据点击几次，来随机选择日期
        :param times:点击次数
        :return:
        """
        for i in range(times):
            nums = random.randint(1, 7)
            if nums == 1:
                self.find_element(*self.EditRepeat1_loc).click()
            elif nums == 2:
                self.find_element(*self.EditRepeat2_loc).click()
            elif nums == 3:
                self.find_element(*self.EditRepeat3_loc).click()
            elif nums == 4:
                self.find_element(*self.EditRepeat4_loc).click()
            elif nums == 5:
                self.find_element(*self.EditRepeat5_loc).click()
            elif nums == 6:
                self.find_element(*self.EditRepeat6_loc).click()
            else:
                self.find_element(*self.EditRepeat7_loc).click()

    """编辑mode的save按钮"""
    EditModeSave_loc = (by.ID, Data.get_elementInfo(20))

    def click_EditModeSave(self):
        """编辑mode的save按钮"""
        self.find_element(*self.EditModeSave_loc).click()

    """编辑mode的Mode name"""
    EditModeName_loc = (by.ID, Data.get_elementInfo(21))

    def click_EditModeName(self):
        """编辑mode的Mode name"""
        self.find_element(*self.EditModeName_loc).click()

    def get_EditModeName(self):
        """获取已选择mode的Mode name"""
        return self.find_element(*self.EditModeName_loc).text

    """编辑mode的Start Time (Open)，记忆添加时time"""
    EditModeStartTime_loc = (by.ID, Data.get_elementInfo(22))

    def get_EditModeStartTime(self):
        """编辑mode的Start Time (Open)，记忆添加时time"""
        return self.find_element(*self.EditModeStartTime_loc)

    """编辑mode的End Time (Close)，记忆添加时time"""
    EditModeEndTime_loc = (by.ID, Data.get_elementInfo(23))

    def get_EditModeEndTime(self):
        """编辑mode的End Time (Close)，记忆添加时time"""
        return self.find_element(*self.EditModeEndTime_loc)

    """编辑mode的delete 按钮"""
    btnEditModeDelete_loc = (by.ID, Data.get_elementInfo(24))

    def click_btnEditModeDelete(self):
        """编辑mode的delete 按钮"""
        self.find_element(*self.btnEditModeDelete_loc).click()

    def func_AddMode(self, message):
        """
        直接点击save保存成功
        :param message:
        :return:
        """
        self.click_btnDeviceSchedulingAdd()  # 点击mode page右上角+号
        self.click_AddModeSave()
        return self.find_toast(message)

    def func_AddMode2(self, times):
        """
        随机选择Mode name，Start time和repeat days，断言Mode name文字
        :param times: 随机选择的repeat days
        :return:
        """
        self.click_btnDeviceSchedulingAdd()  # 承接上面Add Mode1方法，直接Mode list右上角+号
        self.click_AddModeName()
        super().click_SelectMode()  # 选择Mode Name
        mn = self.get_AddModeName()  # 获取选择后的mode name
        self.get_AddModeStartTime().click()
        super().func_NumberPickerComfirm()  # 重新选择start time
        self.click_AddModRepeat(times)  # 随机选择repeat
        self.click_AddModeSave()
        self.find_toast("successfully")  # 先找到提示框后在断言
        return mn in super().get_ModeName()[-1].text  # 获取添加成功显示在最后的mode

    def func_EditMode(self, times):
        """
        随机选择Mode name和repeat days，断言Mode name 文字
        :param times: 随机选择的repeat days
        :return:
        """
        # 承接上面Add Mode后，随机选择item进入edit page
        random.choice(super().get_ModeName()).click()
        self.click_EditModeName()
        super().click_SelectMode()  # 选择Mode Name
        self.click_EditModRepeat(times)  # 随机选择repeat
        mn = self.get_EditModeName()  # 获取选择后的mode name
        self.click_EditModeSave()
        self.find_toast("successfully")  # 先找到提示框后在断言
        return mn in self.get_ModeName()[-1].text  # 获取修改成功显示在最后的mode name

    def func_EditMode2(self,condition):
        """
        只修改endtime》minute为下一分钟，设备执行后，检查mode page该项switch为off
        :return:
        """

        if super().assert_SchedulingAddedList():
            for index, row in enumerate(super().get_ModeName()):
                row.click()  # 直接根据第一个进行编辑，跳转edit page
                self.get_EditModeEndTime().click()
                # 先滑动minute，加1min。避免滑动后改动hour
                super().swipe_NumberPicker_Text(datetime.now().minute, indexs=1)
                # 滑动hour。
                super().swipe_NumberPicker_Text(datetime.now().hour)
                # 等滑动完后点击 time picker confirm按钮
                super().click_popup_timepicker_confirm()
                self.click_EditModeSave()
                self.find_toast("successfully")  # 先找到提示框后在断言
                print(super().get_switchMode()[index].text)
                if condition == super().get_ModeRepeat()[index].text:
                    time.sleep(120)  # 虽然选择+2 min，但重新选择hour会耗时间
                    super().swipe_listPage()
                    LOG.info("编辑ONCE的Mode，等待120s后，关闭switch。实际为%s》》》" % super().get_switchMode()[index].text)
                    return super().get_switchMode()[index].text == "OFF"
                else:
                    time.sleep(120)
                    super().swipe_listPage()
                    LOG.info(
                        "编辑不是ONCE的Mode，等待120s后，switch依然开启。实际为%s》》》" % super().get_switchMode()[index].text)
                    return super().get_switchMode()[index].text == "ON"
        else:
            LOG.info("未找到可编辑的mode项，请检查Mode list page数据》》》")
            return False

    def func_DelMode(self, message):
        """
        在item左滑或者编辑页点击delete按钮进行删除操作
        :param message:
        :return: False
        """
        super().click_ControlMode()  # 点击control page的mode icon

        if super().assert_SchedulingAddedList():
            for i in enumerate(super().get_switchMode()):
                if random.randint(1, 2) == 1:
                    LOG.info("删除操作。左滑动列表项点击delete按钮进行删除》》》")
                    super().leftSwipeClickDel(self.get_switchMode(),self.get_ModeName())  # 随机选择item点击delete
                    super().click_confirm()
                else:
                    LOG.info("删除操作。进入列表项详情页点击底部delete按钮进行删除》》》")
                    random.choice(super().get_ModeName()).click()  # 随机选择item进入edit page
                    self.click_btnEditModeDelete()
                    super().click_confirm()
                self.find_toast(message)
            return super().assert_SchedulingAddedList()
        else:
            LOG.info("未找到可编辑的mode项，请检查Mode list page数据》》》")
            return False
